//
//  SPMineHeaderView.swift
//  Swift-demo
//
//  Created by zcp on 2021/9/28.
//

import UIKit

typealias headImgVAction = (_ url: String)->Void
typealias msgBtnActionBlock = ([String: Any])->Void

class SPMineHeaderView: UICollectionReusableView {
    
    var headImgVAction: headImgVAction?
    var msgBtnActionBlock: msgBtnActionBlock?
    
    
    private lazy var bgImgV: UIImageView = {
        let imgV = UIImageView()
        imgV.contentMode = .scaleAspectFill
        imgV.image = UIImage(named: "mine_sky_bg")
        return imgV
    }()
    
    private lazy var msgBtn: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "noice_pop"), for: .normal)
        btn.tag = 100
        btn.addTarget(self, action: #selector(msgBtnAction), for: .touchUpInside)
        return btn
    }()
    
    private lazy var headBtn: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "sortPlace"), for: .normal)
        btn.backgroundColor = UIColor.white
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tap(tap:)))
        btn.addGestureRecognizer(tapGesture)
        return btn
    }()
    
    private lazy var nameL: UILabel = {
        let label = UILabel()
        label.text = "周先生"
        label.font = UIFont.systemFont(ofSize: 25)
        label.textColor = UIColor.white
        return label
    }()
    
    private lazy var introL: UILabel = {
        let label = UILabel()
        label.text = "每天都进步一点就可以"
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.white
        return label
    }()
    
    private lazy var gifBtn: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "timeAct"), for: .normal)
        btn.layer.backgroundColor = UIColor.init(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.3).cgColor
        btn.layer.cornerRadius = 6
        btn.setTitle("    相信光！！！！！！！！", for:.normal)
        btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 13)
        return btn
    }()
    
    private lazy var btmV: UIView = {
        let v = UIView()
        v.layer.backgroundColor = UIColor.white.cgColor
        v.layer.shadowColor = UIColor.init(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.3).cgColor
        v.layer.shadowOffset = CGSize(width: 0, height: 2)
        v.layer.cornerRadius = 2
        v.layer.shadowOpacity = 1
        v.layer.borderWidth = 1.0
        v.layer.borderColor = UIColor(r: 242, g: 242, b: 242).cgColor
        return v
    }()
    
    private lazy var lineV: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.gray
        return v
    }()
    
    private lazy var stackV: UIStackView = {
        let v = UIStackView()
        v.spacing = 0
        v.alignment = .fill
        v.distribution = .fillEqually
        return v
    }()
    
    private lazy var lb1L: UILabel = {
        let label = UILabel()
        label.text = "未开通\n\n我的VIP"
        label.font = UIFont.systemFont(ofSize: 15)
        label.textColor = UIColor.lightGray
        label.textAlignment = .center
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var lb2L: UILabel = {
        let label = UILabel()
        label.text = "余额 0\n\n我的妖气币"
        label.font = UIFont.systemFont(ofSize: 15)
        label.textColor = UIColor.lightGray
        label.textAlignment = .center
        label.numberOfLines = 0
        return label
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        initUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    @objc private func tap(tap: UITapGestureRecognizer) {
//        print("UITapGestureRecognizer")
        if let back = headImgVAction {
            back("UITapGestureRecognizer")
        }
    }
    
    @objc private func msgBtnAction(sender: UIButton) {
        print(sender.tag)
        if let back = msgBtnActionBlock {
            back(["33": "33"])
        }
    }
    
    func headImgVAction(blcok: @escaping headImgVAction) {
        headImgVAction = blcok
    }
    
    func msgBtnActionBlock(block: @escaping msgBtnActionBlock) {
        msgBtnActionBlock = block
    }
    
    func initUI() {
        addSubview(bgImgV)
        bgImgV.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(UIEdgeInsets(top: 0, left: 0, bottom: 70, right: 0))
        }
        
        addSubview(headBtn)
        headBtn.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.height.width.equalTo(70)
            make.top.equalTo(30)
        }
        headBtn.layer.masksToBounds = true
        headBtn.layer.cornerRadius = 35
        headBtn.layer.borderColor = UIColor.white.cgColor
        headBtn.layer.borderWidth = 3
        
        addSubview(nameL)
        nameL.snp.makeConstraints { make in
            make.left.equalTo(headBtn.snp_right).offset(20)
            make.top.equalTo(headBtn.snp_top).offset(10)
            make.right.equalToSuperview().offset(-10)
        }
        
        addSubview(introL)
        introL.snp.makeConstraints { make in
            make.left.equalTo(nameL)
            make.bottom.equalTo(headBtn.snp_bottom).offset(-10)
            make.right.equalToSuperview().offset(-10)
        }
        
        addSubview(msgBtn)
        msgBtn.snp.makeConstraints { make in
            make.right.equalTo(-15)
            make.top.equalTo(20)
        }
        
        addSubview(gifBtn)
        gifBtn.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.height.equalTo(40)
            make.top.equalTo(headBtn.snp_bottom).offset(20)
        }
        
        addSubview(btmV)
        btmV.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.height.equalTo(100)
            make.top.equalTo(gifBtn.snp_bottom).offset(10)
        }
        
        btmV.addSubview(stackV)
        stackV.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        btmV.addSubview(lineV)
        lineV.snp.makeConstraints { make in
            make.width.equalTo(0.4)
            make.centerX.equalToSuperview()
            make.top.equalTo(16)
            make.bottom.equalTo(-16)
        }
        
        stackV.addArrangedSubview(lb1L)
        stackV.addArrangedSubview(lb2L)
    }
}
